package com.company.cloud.admin.modules.admin.controller;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.company.cloud.admin.modules.admin.controller.req.DeptSavaParam;
import com.company.cloud.admin.modules.admin.controller.req.DeptTreeParam;
import com.company.cloud.admin.modules.admin.controller.req.DeptUpdateParam;
import com.company.cloud.admin.modules.cache.models.DeptModel;
import com.company.cloud.admin.modules.cache.utils.PTUserConvertUtils;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.tree.CloudTree;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.admin.modules.base.entity.DeptInfo;
import com.company.cloud.admin.modules.base.service.DeptInfoService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Authc
@RestController
@RequestMapping("/admin/dept")
@Api(tags = "UserMgr_部门管理")
public class DeptMgrController {
    @Autowired
    private DeptInfoService deptInfoService;

    @GetMapping("/queryTree")
    @ApiOperation(value = "部门树形结构查询", response = DeptModel.class)
    public BaseResult queryTree(DeptTreeParam params) {
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        List<DeptInfo> depts = deptInfoService.list();
        ;
        CloudTree<DeptInfo, DeptModel> tree = new CloudTree<DeptInfo, DeptModel>() {
            @Override
            public boolean isTop(DeptModel entity) {
                if (entity.getParentId() == null || entity.getParentId() == 0) {
                    return true;
                } else {
                    return false;
                }
            }

            @Override
            public boolean isParent(DeptModel parentObj, DeptModel childObj) {
                return parentObj.getId().intValue() == childObj.getParentId().intValue();
            }

            @Override
            public DeptModel convert(DeptInfo entity) {
                return PTUserConvertUtils.getDeptModel(entity);
            }
        };
        return BaseResult.ok(tree.tree(depts));
    }

    @GetMapping("/get")
    @ApiOperation(value = "部门详情信息", response = DeptModel.class)
    public BaseResult get(@RequestParam("id") Integer id) {
        DeptInfo entity = deptInfoService.getById(id);
        DeptModel data = PTUserConvertUtils.getDeptModel(entity);
        return BaseResult.ok(data);
    }

    @PostMapping("/save")
    @ApiOperation(value = "部门信息新增", response = DeptModel.class)
    @StaffLogc(title = "部门信息新增", action = "部门信息新增", actionAdo = "部门信息")
    public BaseResult save(@RequestBody DeptSavaParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        DeptInfo entity = new DeptInfo();
        entity.setName(params.getName());
        entity.setType(params.getType());
        entity.setAddr(params.getAddr());
        entity.setLlType(params.getLlType());
        entity.setLongitude(params.getLongitude());
        entity.setLatitude(params.getLatitude());
        entity.setParentId(params.getParentId());
        entity.setDataAuth(params.getDataAuth());
        entity.setRemark(params.getRemark());
        entity.addCreateParam(userInfo);
        entity.addEditParam(userInfo);
        deptInfoService.save(entity);
        DeptModel data = PTUserConvertUtils.getDeptModel(entity);
        return BaseResult.ok(data);
    }

    @PostMapping("/update")
    @ApiOperation(value = "部门信息修改", response = DeptModel.class)
    @StaffLogc(title = "部门信息修改", action = "部门信息修改", actionAdo = "部门信息")
    public BaseResult update(@RequestBody DeptUpdateParam params) {
        ValidatorUtils.validate(params);
        DeptInfo entity = deptInfoService.getById(params.getId());
        if (null == entity) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        entity.setName(params.getName());
        entity.setType(params.getType());
        entity.setAddr(params.getAddr());
        entity.setLlType(params.getLlType());
        entity.setLongitude(params.getLongitude());
        entity.setLatitude(params.getLatitude());
        entity.setParentId(params.getParentId());
        entity.setDataAuth(params.getDataAuth());
        entity.setRemark(params.getRemark());
        entity.addEditParam(userInfo);
        deptInfoService.updateById(entity);
        DeptModel data = PTUserConvertUtils.getDeptModel(entity);
        return BaseResult.ok(data);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "部门信息删除", response = BaseResult.class)
    @StaffLogc(title = "部门信息删除", action = "部门信息删除", actionAdo = "部门信息")
    public BaseResult delete(@RequestParam("id") Integer id) {
        DeptInfo entity = deptInfoService.getById(id);
        if (null == entity) {
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        int childrenNum = deptInfoService.count(
                new QueryWrapper<DeptInfo>()
                        .eq("parent_id", id));
        if (childrenNum > 0) {
            throw new BusinessException("该部门存在子节点");
        }
        deptInfoService.removeById(id);
        return BaseResult.ok();
    }

}
